<template>
  <el-card style="margin-top: 15px;">
    <el-steps :active="active" simple>
      <el-step
        v-for="(step, index) in steps"
        :key="index"
        :title="step.label"
        :icon="step.icon"
        @click.native="active=index"
        style="cursor: pointer"
      ></el-step>
    </el-steps>
    <el-form label-width="80px" style="margin-top: 20px">
      <div v-show="active === 0">
        <el-form-item label="名称">
          <el-input v-model="course.courseName"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="course.brief"></el-input>
        </el-form-item>
        <el-form-item label="讲师姓名">
          <el-input v-model="course.teacherDTO.teacherName"></el-input>
        </el-form-item>
        <el-form-item label="职位">
          <el-input v-model="course.teacherDTO.position"></el-input>
        </el-form-item>
        <el-form-item label="讲师简介">
          <el-input v-model="course.teacherDTO.description"></el-input>
        </el-form-item>
        <el-form-item label="课程概述">
          <el-input
            style="width:49%"
            maxlength="20"
            show-word-limit
            v-model="course.previewFirstField"
            placeholder="概述1"
          ></el-input>
          <el-input
            style="width:49%;margin-left: 2%;"
            maxlength="20"
            show-word-limit
            v-model="course.previewSecondField"
            placeholder="概述2"
          ></el-input>
        </el-form-item>
        <el-form-item label="课程排序">
          <el-input-number :min="0" v-model="course.sortNum"></el-input-number>
        </el-form-item>
      </div>
      <div v-show="active === 1">
        <el-form-item label="课程封面">
          <course-image v-model="course.courseListImg" />
        </el-form-item>
        <el-form-item label="介绍封面">
          <course-image v-model="course.courseImgUrl" />
        </el-form-item>
      </div>
      <div v-show="active === 2">
        <el-form-item label="售卖价格">
          <el-input v-model="course.discounts">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="商品原价">
          <el-input v-model="course.price">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="销量">
          <el-input v-model="course.sales">
            <template slot="append">单</template>
          </el-input>
        </el-form-item>
        <el-form-item label="活动标签">
          <el-input
            v-model="course.discountsTag"
            maxlength="4"
            show-word-limit
          ></el-input>
        </el-form-item>
      </div>
      <div v-show="active === 3">
        <el-form-item label="限时秒杀开关">
          <el-switch
            v-model="course.activityCourse"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <template v-if="course.activityCourse">
          <el-form-item label="开始时间">
            <el-date-picker v-model="course.activityCourseDTO.beginTime" type="date" placeholder="选择日期时间" style="width: 100%;"></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间">
            <el-date-picker v-model="course.activityCourseDTO.endTime" type="date" placeholder="选择日期时间" style="width: 100%;"></el-date-picker>
          </el-form-item>
          <el-form-item label="秒杀价">
            <el-input v-model="course.activityCourseDTO.amount">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="秒杀库存">
            <el-input v-model="course.activityCourseDTO.stock">
              <template slot="append">个</template>
            </el-input>
          </el-form-item>
        </template>
      </div>
      <div v-if="active === 4">
        <el-form-item label="课程详情">
          <text-editor v-model="course.courseDescriptionMarkDown" />
        </el-form-item>
        <el-form-item label="是否发布">
          <el-switch
            v-model="course.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </el-form-item>
      </div>
      <el-form-item v-show="active>=0&&active<4">
        <el-button @click="active++">下一步</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
import { saveOrUpdateCourse, getCourseById } from '@/services/course'
import CourseImage from './course-image.vue'
import TextEditor from '@/components/text-editor/index.vue'

export default Vue.extend({
  name: 'CourseCreateOrUpdate',
  components: {
    CourseImage,
    TextEditor
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [String, Number]
    }
  },
  data () {
    return {
      active: 0,
      steps: [
        { label: '基本信息', icon: 'el-icon-edit' },
        { label: '课程封面', icon: 'el-icon-upload' },
        { label: '销售信息', icon: 'el-icon-star-on' },
        { label: '秒杀活动', icon: 'el-icon-timer' },
        { label: '课程详情', icon: 'el-icon-info' }
      ],
      imageUrl: '',
      isSeckill: false,
      course: {
        // id: 0,
        courseName: '',
        brief: '',
        teacherDTO: {
          // id: 0,
          // courseId: 0,
          teacherName: '',
          position: '',
          description: ''
        },
        courseDescriptionMarkDown: '',
        price: '',
        discounts: '',
        priceTag: '',
        discountsTag: '',
        // isNew: true,
        // isNewDes: '',
        courseListImg: '',
        courseImgUrl: '',
        sortNum: '',
        previewFirstField: '',
        previewSecondField: '',
        status: 0,
        sales: '',
        activityCourse: false,
        activityCourseDTO: {
          // id: 0,
          // courseId: 0,
          beginTime: '',
          endTime: '',
          amount: '',
          stock: ''
        }
      }
    }
  },
  created () {
    if (this.courseId) {
      this.loadCourse()
    }
  },
  methods: {
    async loadCourse () {
      const { data } = await getCourseById(this.courseId)
      this.course = data.data
    },

    async handleSave () {
      await saveOrUpdateCourse(this.course)
      this.$router.push({
        name: 'course'
      })
    }
  }
})
</script>

<style lang="scss" scoped>

</style>
